import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Res,
  Query,
  ParseIntPipe,
  Req,
} from '@nestjs/common';
import { ArticlesService } from './articles.service';
import { CreateArticleDto } from './dto/create-article.dto';
import { UpdateArticleDto } from './dto/update-article.dto';
import { TagsService } from '../tags/tags.service';
import { ArticleTagService } from '../article-tag/article-tag.service';
import { articles } from '@prisma/client';

@Controller()
export class ArticlesController {
  constructor(
    private readonly articlesService: ArticlesService,
    private readonly tagsService: TagsService,
    private readonly articleTagService: ArticleTagService,
  ) {}

  @Post()
  async createArticle(
    @Body() article: CreateArticleDto,
    @Req() req,
    @Res() res,
  ) {
    const { user_id, username } = req.user;
    const { tags, ...otherInfo } = article;
    const newArticle = await this.articlesService.createArticle(
      { user_id, username },
      otherInfo,
    );
    // 处理标签
    if (tags && tags?.length > 0) {
      const addTags = await this.tagsService.handleTags(tags);
      // 添加标签
      if (addTags.length > 0) {
        await this.articleTagService.articleAddTag({
          article_id: newArticle.article_id,
          tags: addTags,
        });
      }
    }
    res.send({
      code: 0,
      msg: '创建成功',
    });
  }

  @Patch(':article_id')
  async updateArticle(
    @Param('article_id', ParseIntPipe) article_id: number,
    @Body() updateArticleDto: UpdateArticleDto,
    @Req() req,
    @Res() res,
  ) {
    await this.articlesService.identifyAuth(req.user.user_id, article_id);
    const { tags, ...otherInfo } = updateArticleDto;
    // 处理标签
    if (tags && tags?.length > 0) {
      const addTags = await this.tagsService.handleTags(tags);
      // 添加标签
      if (addTags.length > 0) {
        await this.articleTagService.articleAddTag({
          article_id,
          tags: addTags,
        });
      }
    }
    await this.articlesService.updateArticle(article_id, otherInfo);
    res.send({
      code: 0,
      msg: '编辑成功',
    });
  }

  @Get()
  async getSelfArticles(
    @Query('page', ParseIntPipe) page,
    @Query('pageSize', ParseIntPipe) pageSize,
    @Query('keywords') keywords,
    @Query() query,
    @Req() req,
  ) {
    let list: articles[] | { article_id: number; title: string }[] = [];
    let total = 0;
    if (!query.type) {
      [list, total] = await this.articlesService.getSimpleArticleInfo({
        page,
        pageSize,
        keywords,
      });
    } else {
      [list, total] = await this.articlesService.getSelfArticles({
        user_id: req.user.user_id,
        page,
        pageSize,
        keywords,
        top_up: query.topUp && +query.topUp,
        popular: query.popular && +query.popular,
        original: query.original && +query.original,
        type: query.type && +query.type,
        page_view: query.pageView && +query.pageView,
        words_num: query.wordsNum && +query.wordsNum,
        comment_num: query.commentNum && +query.commentNum,
        create_time: query.createTime,
        update_time: query.updateTime,
      });
    }

    return {
      list,
      total,
    };
  }

  @Get(':article_id')
  async getSelfArticle(@Param('article_id') article_id: string, @Req() req) {
    await this.articlesService.identifyAuth(req.user.user_id, +article_id);
    return await this.articlesService.getSelfArticle(+article_id);
  }

  @Delete(':article_id')
  async reOneArticle(
    @Param('article_id') article_id: string,
    @Req() req,
    @Res() res,
  ) {
    await this.articlesService.identifyAuth(req.user.user_id, +article_id);
    await this.articlesService.reOneArticle(+article_id);
    res.send({
      code: 0,
      msg: '删除成功',
    });
  }
}
